package cc.news

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.Bindable
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import cc.NewsModel
import cc.bean.NewsInfo
import cc.common.base.BaseObserver
import cc.news.databinding.FragmentNewsBinding
import cc.news.databinding.FragmentNewsVmBinding
import cc.news.databinding.ListItemNewsBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.android.databinding.basicsample.util.ObservableViewModel
import com.trello.rxlifecycle3.components.support.RxFragment
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class NewsVMFragment : RxFragment() {

    private val vm by lazy {
        ViewModelProvider(this).get(NewsViewModel::class.java)
    }
    private val adapter by lazy {
        object : BaseQuickAdapter<NewsInfo, BaseViewHolder>(R.layout.list_item_news) {
            override fun convert(helper: BaseViewHolder, item: NewsInfo?) {
                DataBindingUtil.getBinding<ListItemNewsBinding>(helper.itemView)?.setVariable(BR.news, item)
            }

            override fun getItemView(layoutResId: Int, parent: ViewGroup?): View {
                val binding = DataBindingUtil.inflate<ListItemNewsBinding>(LayoutInflater.from(context), R.layout.list_item_news, parent, false)
                return binding.root
            }
        }.also {
            it.setOnItemClickListener { adapter, view, position ->
                vm.readCount.value = (vm.readCount.value ?: 0) + 1
                vm.readTip = "阅读数${vm.readCount.value}"

                it.getItem(position)?.isRead = true
                it.notifyItemChanged(position)
                Toast.makeText(context, "你点击了$position", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = DataBindingUtil.inflate<FragmentNewsVmBinding>(inflater, R.layout.fragment_news_vm, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        DataBindingUtil.getBinding<FragmentNewsVmBinding>(view)?.run {
            rvNews.layoutManager = LinearLayoutManager(context)
            rvNews.adapter = adapter
            this.readTip2 = vm
            vm.getNewsList().compose(bindToLifecycle()).observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(object : BaseObserver<List<NewsInfo>>(this@NewsVMFragment) {
                        override fun doNext(t: List<NewsInfo>?) {
                            adapter.setNewData(t)
                        }
                    })
            vm.readTip = "未开始阅读"
        }
    }


}

class NewsViewModel : ObservableViewModel() {
    var readCount = MutableLiveData<Int>()
    @Bindable
    var readTip = "未阅读"
        set(value) {
            field = value
            notifyPropertyChanged(BR.readTip)
        }

    fun getNewsList() = NewsModel().news
}